package com.juzipi.controller;

import com.juzipi.common.core.BaseController;
import com.juzipi.common.model.PageBody;
import com.juzipi.common.tools.Result;
import com.juzipi.inter.pojo.User;
import com.juzipi.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@Api("用户")
public class UserController extends BaseController {


    @Autowired
    private UserService userService;



    /**
     * 根据id查询用户
     * @param userId
     * @return
     */
    @PreAuthorize("hasAnyRole('user','admin','super')")
    @GetMapping("getUser")
    @ApiOperation("根据id查询用户")
    public Result queryUserById(@RequestParam(value = "userId") Long userId) {
        return JudgeResult(userService.queryUserById(userId));
    }


    @PreAuthorize("hasAnyRole('user','admin','super')")
    @PostMapping("user")
    @ApiOperation("获取所有用户分页")
    public Result queryAllUserAndRole(@RequestBody PageBody pageBody) {
        return JudgeResult(userService.queryAllUserAndRole(pageBody));
    }


    @PreAuthorize("hasAnyRole('user','admin','super')")
    @PostMapping("like")
    @ApiOperation("获取所有用户模糊查询的分页结果集")
    public Result queryAllUserAndRoleLike(@RequestBody PageBody pageBody) {
        return JudgeResult(userService.queryAllUserAndRoleLike(pageBody));
    }



    @PreAuthorize("hasAnyRole('user','admin','super')")
    @PutMapping("modify")
    @ApiOperation("修改用户信息")
    public Result modifyUser(@RequestBody User user) {
        Object object = userService.modifyUser(user);
        return checkUniqueResult(object);
    }



    @PreAuthorize("hasAnyRole('user','admin','super')")
    @PostMapping("sort")
    @ApiOperation("根据角色名查询此角色所有的用户")
    public Result queryUserBySort(@RequestBody PageBody pageBody) {
        return JudgeResult(userService.queryUserBySort(pageBody));
    }



    @PreAuthorize("hasAnyRole('admin','super')")
    @DeleteMapping("remove")
    @ApiOperation("根据用户id批量删除用户")
    public Result removeUserByIds(@RequestParam(value = "userIds") List<Long> userIds) {
        return toResult(userService.removeUserByIds(userIds));
    }




}
